package com.zany.system.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.vo.ParameterSingleVO;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.file.FileUtil;
import com.zany.util.param.SysParamKey;
import com.zany.util.service.SingleParamUtilService;

public class ExeclToTxtAction extends AbstractActionSupport{

	private static final long serialVersionUID = -1894977676965077089L;
	private static Log logger = LogFactory.getLog(ExeclToTxtAction.class);
	private SingleParamUtilService singleParamUtilService;
	private ParameterSingleVO parameterSingleVO = new ParameterSingleVO();
	private File file;
	private String fileName;
	private String contentType;
	private StringBuffer stringBuffer = new StringBuffer();
	
	public void execlToTxt() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ExeclToTxtAction.execlToTxt()");}
		response = ServletActionContext.getResponse();
		String[] fileRealName = fileName.split("\\.");
		if(fileRealName.length!=2){
			String returnMsg = "请确认批量文件名！";
			AjaxTools.exAjax("{success:false,msg:\""+returnMsg+"\"}",response);
		}else if(!fileRealName[1].equalsIgnoreCase("xls")){
			String returnMsg = "批量文件必须为xls！";
			AjaxTools.exAjax("{success:false,msg:\""+returnMsg+"\"}",response);
		}
		String fileNameReal = fileRealName[0]+".txt";
		Workbook workbook = null;
		Sheet sheet =null;
		Cell cell = null;
		
		try {
			workbook = Workbook.getWorkbook(file);
		} catch (Exception e) {
			String returnMsg = "未找到转换文件！";
			AjaxTools.exAjax("{success:false,msg:\""+returnMsg+"\"}",response);
		}
		sheet= workbook.getSheet(0);
		int columnCount=sheet.getColumns();
		int rowCount=sheet.getRows();
		for (int i = 1; i <rowCount; i++) {
			for (int j = 0; j <columnCount; j++) {//注意，这里的两个参数，第一个是表示列的，第二才表示行
				cell=sheet.getCell(j, i); //要根据单元格的类型分别做处理，否则格式化过的内容可能会不正确	
				if(j==1){
					String[] cells = cell.getContents().split("-");
					if(cells.length == 3){
						stringBuffer.append(cells[0]);
						stringBuffer.append("|");
						stringBuffer.append(cells[1]);
						stringBuffer.append("|");
						stringBuffer.append(cells[2]);
						stringBuffer.append("|");
					}else{
						stringBuffer.append(cell.getContents());
						stringBuffer.append("|");
					}
				}else{
					stringBuffer.append(cell.getContents());
					stringBuffer.append("|");
				}				
			}
			stringBuffer.append("\r\n");
		}
		//关闭它，否则会有内存泄露
		workbook.close();
		parameterSingleVO.setParamCode(SysParamKey.EXECL_TXT.getKey());
		String path = singleParamUtilService.getSysParams(parameterSingleVO);
		FileUtil.stringToTxt(stringBuffer.toString(), path+fileNameReal);
		AjaxTools.exAjax("{success:true,msg:\""+fileNameReal+"\"}",response);
	}
	
	public void downloadTxt() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ExeclToTxtAction.downloadTxt()");}
		response = ServletActionContext.getResponse();
		String txtName = "";
		txtName = request.getParameter("txtName");
		try {
//			txtName = request.getParameter("txtName");
			txtName = new String(request.getParameter("txtName").getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		parameterSingleVO.setParamCode(SysParamKey.EXECL_TXT.getKey());
		String path = singleParamUtilService.getSysParams(parameterSingleVO);
		FileUtil.downFile(path+txtName, txtName, response);
	}
	
	public String getFileFileName() {
		return fileName;
	}
	public void setFileFileName(String fileName) {
		this.fileName = fileName;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
	}
	public SingleParamUtilService getSingleParamUtilService() {
		return singleParamUtilService;
	}
	public void setSingleParamUtilService(
			SingleParamUtilService singleParamUtilService) {
		this.singleParamUtilService = singleParamUtilService;
	}
}
